package com.vcredit.creditcat.service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.vcredit.creditcat.model.ProductSupplementItem;
import com.vcredit.creditcat.persistence.base.ProductSupplementItemMapper;
import com.vcredit.creditcat.util.Utility;
/**
 * 
 **/
@Service
public class ProductSupplementItemService {
	@Autowired
    private ProductSupplementItemMapper mapper;
    
	@CacheEvict(key = "'config:supplementItems'", value = "default")
    public void insert(ProductSupplementItem obj){
    	mapper.insert(obj);
    }
    
	@CacheEvict(key = "'config:supplementItems'", value = "default")
    public void update(ProductSupplementItem obj){
    	mapper.update(obj);
    }
    
    private List<ProductSupplementItem> findAll(){
		return mapper.findAll();
	}
    
    @Cacheable(key = "'config:supplementItems'", value = "default")
    public Map<Long, ProductSupplementItem> findAllForMap() {
    	Map<Long, ProductSupplementItem> result = new HashMap<Long, ProductSupplementItem>();
    	List<ProductSupplementItem> all = this.findAll();
    	for (ProductSupplementItem productSupplementItem : all) {
    		result.put(productSupplementItem.getItemId(), productSupplementItem);
		}
    	return result;
    }
	
	public List<ProductSupplementItem> find(ProductSupplementItem obj){
		return mapper.find(obj);
	}
	
	public ProductSupplementItem findById(Long id){
		return mapper.findById(id);
	}

	public ProductSupplementItem findByItemCode(Integer authItemCodeIntVal) {
		ProductSupplementItem item = new ProductSupplementItem();
		item.setItemCode(authItemCodeIntVal);
		List<ProductSupplementItem> list = this.find(item);
		if(Utility.isNullOrEmpty(list))
			return null;
		return list.get(0);
	}
}